
/***************************************************************************************************************************

This part of the dofile is copy-pasted from the dofile "joint_impact_prepare_data.do" from Finkelstein et al. (2016, NEJM).
We made three insignificant changes: 
1. The working directory was added and the folders to obtain the data were changed.
2. We do not save the final data as "data_for_analysis.dta".
3. We make hh* and draw* dummies with the code obtained from "timing_non_parametric_estimates.do".

***************************************************************************************************************************/

cd "$INDTA"

cap program drop main joint_impact_data

program main
    joint_impact_data
end

program joint_impact_data
    use oregonhie_patterns_vars.dta, clear
    gen ed_sample = 1
    gen overlap_sample = 1 if ed_sample==1 & sample_inp_resp==1 & doc_any_incl_probe_inp!=.
    count if overlap_sample == 1
    assert r(N) == 10156
    count if ed_sample == 1
    assert r(N) == 24646

    gen ed_admin         =  any_inp_match_ed
    gen ed_inp           =  ed_any_incl_probe_inp  if overlap_sample == 1
    gen doc_inp          =  doc_any_incl_probe_inp if overlap_sample == 1
    gen ed_admin_doc_inp = (any_inp_match_ed==1 & doc_any_incl_probe_inp==1) if overlap_sample == 1
    gen ed_inp_doc_inp   = (ed_any_incl_probe_inp==1 & doc_any_incl_probe_inp==1) ///
        if doc_any_incl_probe_inp!=. & ed_any_incl_probe_inp!=. & overlap_sample == 1

    label var ed_admin          "Went to the ED (ED admin data)"
    label var ed_inp            "Went to the ED (inperson survey, defined for ED/Inp overlap)"
    label var doc_inp           "Went to doctor (inperson survey, defined for ED/Inp overlap)"
    label var ed_admin_doc_inp  "Went to the ED (ED admin data)   and the doctor (inperson survey)"
    label var ed_inp_doc_inp    "Went to the ED (inperson survey) and the doctor (inperson data)"

    assert any_inp_match_ed!=. if overlap_sample == 1
    assert doc_any_incl_probe_inp!=. if overlap_sample == 1
    assert any_inp_match_ed==1 if ed_admin_doc_inp == 1
    assert doc_any_incl_probe_inp==1 if ed_admin_doc_inp == 1

    *save OHIE_Data/data_for_analysis.dta, replace
end

*Execute
main

*Added from timing_non_parametric_estimates.do
*And added the option noomit ourselves
xi, prefix(hh) noomit i.numhh_list
xi, prefix(draw) noomit i.draw_lottery



/***************************************************************************************************************************

This part of the dofile is our code: Create Z, D, Y, and globals per application

***************************************************************************************************************************/

***Data prep for main analysis
gen z=treatment

gen d1=medicaid_all_180p_period_180+medicaid_all_180p_period_360-(medicaid_all_180p_period_180*medicaid_all_180p_period_360)
gen d2=medicaid_all_180p_period_540+medicaid_all_180p_period_720-(medicaid_all_180p_period_540*medicaid_all_180p_period_720)

assert medicaid_all_180p_period_180!=. & medicaid_all_180p_period_360!=. & medicaid_all_180p_period_540!=. & medicaid_all_180p_period_720!=.

gen y=(any_visit_180p_180+any_visit_180p_360+any_visit_180p_540+any_visit_180p_720 >= 1) 

assert any_visit_180p_180!=. & any_visit_180p_360!=. & any_visit_180p_540!=. & any_visit_180p_720!=.

global controls 	drawdraw_lo_2 drawdraw_lo_3 drawdraw_lo_4 drawdraw_lo_5 drawdraw_lo_6 drawdraw_lo_7 drawdraw_lo_8 ///
					hhnumhh_lis_2 hhnumhh_lis_3 preperiod_any_visits 
global weight 		weight_360days
global clustervar	household_id

assert $weight ==1 // no weights

global controls_s12 drawdraw_lo_2_s1 drawdraw_lo_2_s2 drawdraw_lo_3_s1 drawdraw_lo_3_s2 drawdraw_lo_4_s1 drawdraw_lo_4_s2 drawdraw_lo_5_s1 ///
					drawdraw_lo_5_s2 drawdraw_lo_6_s1 drawdraw_lo_6_s2 drawdraw_lo_7_s1 drawdraw_lo_7_s2 drawdraw_lo_8_s1 drawdraw_lo_8_s2 ///
					hhnumhh_lis_2_s1 hhnumhh_lis_2_s2 hhnumhh_lis_3_s1 hhnumhh_lis_3_s2 preperiod_any_visits_s1 preperiod_any_visits_s2

***Data prep for analysis per x
gen 	preperiod_any_visits_0=0, after(preperiod_any_visits)
replace preperiod_any_visits_0=1 if preperiod_any_visits==0
gen 	preperiod_any_visits_1=0, after(preperiod_any_visits)
replace preperiod_any_visits_1=1 if preperiod_any_visits==1

global controlsx1  	drawdraw_lo_1 drawdraw_lo_2 drawdraw_lo_3 drawdraw_lo_4 drawdraw_lo_5 drawdraw_lo_6 drawdraw_lo_7 drawdraw_lo_8 
global controlsx2  	hhnumhh_lis_1 hhnumhh_lis_2 hhnumhh_lis_3 
global controlsx3  	preperiod_any_visits_0 preperiod_any_visits_1
						
global clustervarx 	household_id					

***No important var is missing



/***************************************************************************************************************************
***Appendix: Replicate Table A.6 of Finkelstein et al. (2016)

local n=1
foreach p in 180 360 540 720 {
		
	eststo m`n': reg medicaid_all_180p_period_`p' treatment $controls [pw = weight_`p'days ], cluster($clustervar ) // fs
	local n=`n'+1
	eststo m`n': reg any_visit_180p_`p' treatment $controls [pw = weight_`p'days ], cluster($clustervar ) // rf
	local n=`n'+1
	eststo m`n': ivregress 2sls any_visit_180p_`p' $controls (medicaid_all_180p_period_`p' = treatment) [pw = weight_`p'days ], cluster($clustervar ) // ss
	local n=`n'+1
}

esttab m1 m4 m7 m10, keep(treatment) b(3) se(3) starlevel(* 0.1 ** 0.05 *** 0.01) ///
												nonotes label title("Replication of first stage of Finkelstein et al. (2016) Table A6")
esttab m2 m5 m8 m11, keep(treatment) b(3) se(3) starlevel(* 0.1 ** 0.05 *** 0.01) ///
												nonotes label title("Replication of reduced form of Finkelstein et al. (2016) Table A6")
esttab m3 m6 m9 m12, keep(medicaid_all_180p_period_*) b(3) se(3) starlevel(* 0.1 ** 0.05 *** 0.01) ///
												nonotes label title("Replication of second stage of Finkelstein et al. (2016) Table A6")
eststo clear
***************************************************************************************************************************/

